package com.co.video.service.impl;

import com.co.video.mapper.VideoStateMapper;
import com.co.video.pojo.VideoState;
import com.co.video.service.VideoStateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author ldl.plus
 * @date 2020年06月22日  0:41
 * TvVideoState业务层接口实现类
 */
@Service
public class VideoStateServiceImpl implements VideoStateService {

    @Autowired
    private VideoStateMapper videoStateMapper;


    /**
     * TvVideoState条件+分页查询
     *
     * @param videoState 查询条件
     * @param page       页码
     * @param size       页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<VideoState> findPage(VideoState videoState, int page, int size) {
        //分页
        PageHelper.startPage(page, size);
        //搜索条件构建
        Example example = createExample(videoState);
        //执行搜索
        return new PageInfo<VideoState>(videoStateMapper.selectByExample(example));
    }

    /**
     * TvVideoState分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<VideoState> findPage(int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        //分页查询
        return new PageInfo<VideoState>(videoStateMapper.selectAll());
    }

    /**
     * TvVideoState条件查询
     *
     * @param videoState
     * @return
     */
    @Override
    public List<VideoState> findList(VideoState videoState) {
        //构建查询条件
        Example example = createExample(videoState);
        //根据构建的条件查询数据
        return videoStateMapper.selectByExample(example);
    }


    /**
     * TvVideoState构建查询对象
     *
     * @param videoState
     * @return
     */
    public Example createExample(VideoState videoState) {
        Example example = new Example(VideoState.class);
        Example.Criteria criteria = example.createCriteria();
        if (videoState != null) {
            // 主键
            if (!StringUtils.isEmpty(videoState.getId())) {
                criteria.andEqualTo("id", videoState.getId());
            }
            // 视频表id
            if (!StringUtils.isEmpty(videoState.getAid())) {
                criteria.andEqualTo("aid", videoState.getAid());
            }
            // 观看次数
            if (!StringUtils.isEmpty(videoState.getView())) {
                criteria.andEqualTo("view", videoState.getView());
            }
            // 回复
            if (!StringUtils.isEmpty(videoState.getReply())) {
                criteria.andEqualTo("reply", videoState.getReply());
            }
            // 弹幕数量
            if (!StringUtils.isEmpty(videoState.getDanmaku())) {
                criteria.andEqualTo("danmaku", videoState.getDanmaku());
            }
            // 收藏数量
            if (!StringUtils.isEmpty(videoState.getFavorite())) {
                criteria.andEqualTo("favorite", videoState.getFavorite());
            }
            // 点赞数量
            if (!StringUtils.isEmpty(videoState.getLike())) {
                criteria.andEqualTo("like", videoState.getLike());
            }
            // 踩数量（不喜欢）
            if (!StringUtils.isEmpty(videoState.getDislike())) {
                criteria.andEqualTo("dislike", videoState.getDislike());
            }
            // 现在排行
            if (!StringUtils.isEmpty(videoState.getNowRank())) {
                criteria.andEqualTo("nowRank", videoState.getNowRank());
            }
            // 历史排行
            if (!StringUtils.isEmpty(videoState.getHisRank())) {
                criteria.andEqualTo("hisRank", videoState.getHisRank());
            }
            // 分享数量
            if (!StringUtils.isEmpty(videoState.getShare())) {
                criteria.andEqualTo("share", videoState.getShare());
            }
            // 创建时间
            if (!StringUtils.isEmpty(videoState.getCreateTime())) {
                criteria.andEqualTo("createTime", videoState.getCreateTime());
            }
            // 修改时间
            if (!StringUtils.isEmpty(videoState.getUpdateTime())) {
                criteria.andEqualTo("updateTime", videoState.getUpdateTime());
            }
        }
        return example;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        videoStateMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改TvVideoState
     *
     * @param videoState
     */
    @Override
    public void update(VideoState videoState) {
        videoStateMapper.updateByPrimaryKey(videoState);
    }

    /**
     * 增加TvVideoState
     *
     * @param videoState
     */
    @Override
    public void add(VideoState videoState) {
        videoStateMapper.insert(videoState);
    }

    /**
     * 根据ID查询TvVideoState
     *
     * @param id
     * @return
     */
    @Override
    public VideoState findById(String id) {
        return videoStateMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询TvVideoState全部数据
     *
     * @return
     */
    @Override
    public List<VideoState> findAll() {
        return videoStateMapper.selectAll();
    }

    @Override
    public VideoState findByAId(String aid) {
        VideoState videoState = new VideoState();
        videoState.setAid(aid);
        return videoStateMapper.selectOne(videoState);
    }
}
